package com.vois.jack.btmgr.common;

import android.util.Log;
import com.vois.jack.btmgr.blebase.BleDevice;
import com.vois.jack.btmgr.blebase.BleDeviceBuilder;
import com.vois.jack.btmgr.classicbase.BtDevice;
import com.vois.jack.btmgr.classicbase.BtDeviceBuilder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeviceRegisterHelper {
    private static final String TAG  = DeviceRegisterHelper.class.getName();
    private static final int BT_TYPE = 0;
    private static final int BLE_TYPE = 1;
    private static final Map<Integer, BurstSourceTypeInfo> burstSourceTypeInfoList = new HashMap<>();

    static public boolean registerWithProfile(String profileString) {
        String finalProfileString = profileString != null ? profileString : DefaultRegisterProfile.defaultProfiles;
        try {
            JSONObject devices = new JSONObject(finalProfileString);
            JSONArray profileList = devices.getJSONArray("devices");

            for (int i = 0; i < profileList.length(); i++) {
                JSONObject profile = profileList.getJSONObject(i);
                int deviceType = profile.getInt("type");
                String namePattern = profile.getString("name_pattern");
                String className = profile.getString("class_name");
                int burstType = profile.getInt("burst_type");
                boolean scanPresented = deviceType == BLE_TYPE && profile.getBoolean("scan_presented");

                Log.d(TAG, "registerWithProfile: " + className + ":" + namePattern + ":" + scanPresented);

                try {
                    Class<?> deviceClass = Class.forName(className);
                    if (deviceType == BT_TYPE) {
                        BtDeviceBuilder.unregisterBtDeviceClass(namePattern);
                        BtDeviceBuilder.registerBtDeviceClass(namePattern, (Class<? extends BtDevice>) deviceClass, burstType);
                    } else {
                        BleDeviceBuilder.unregisterBleDeviceClass(namePattern);
                        BleDeviceBuilder.registerBleDeviceClass(namePattern, (Class<? extends BleDevice>) deviceClass, burstType, scanPresented);
                    }
                }catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                    Log.d(TAG, "class of " + className + " not found." + e2.getMessage());
                }
            }

            JSONArray burstSourceTypeList = devices.getJSONArray("burst_options");
            for (int i = 0; i < burstSourceTypeList.length(); i++) {
                JSONObject burstSource = burstSourceTypeList.getJSONObject(i);
                BurstSourceTypeInfo info = new BurstSourceTypeInfo();
                info.burstSourceType = burstSource.getInt("sourceType");
                info.burstSourceName = burstSource.getString("name");
                info.burstSourceUrl = burstSource.getString("sourceUrl");
                burstSourceTypeInfoList.put(info.burstSourceType, info);
            }

            return true;
        }catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "registerWithProfile: json problem " + e.getMessage());
        }

        return false;
    }

    static public String getBurstSourceTypeUrl(int burstSourceType) {
        if (burstSourceTypeInfoList.containsKey(burstSourceType)) {
            return burstSourceTypeInfoList.get(burstSourceType).burstSourceUrl;
        }

        return "";
    }
}
